package controller

import (
	"sems/dao"
	"sems/logic"
	"sems/util/encryptPassword"
	"sems/util/jwt"
	"sems/util/oss"
	"sems/util/resp"
	"strconv"

	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

// Register 新用户注册
func Register(c *gin.Context) {
	//注册账户之前判断数据库中有没有该账户
	username := c.Request.FormValue("username")
	password := c.Request.FormValue("password")
	rPassword := c.Request.FormValue("r_password")
	user, _ := logic.CheckUser(username)
	if user.UserID > 0 {
		zap.L().Error("用户名已存在")
		resp.ResponseError(c, resp.CodeUserExist)
		return
	} else {
		if rPassword != password {
			zap.L().Error("两次输入的密码不一致")
			resp.ResponseErrorWithMsg(c, resp.CodeInvalidParams, "两次输入的密码不一致")
			return
		}
		if err := logic.SaveUser(username, password); err != nil {
			zap.L().Error("注册用户失败")
			resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, err.Error())
			return
		}
	}
	resp.ResponseSuccess(c, resp.CodeSuccess)
}

// Login 用户登录
func Login(c *gin.Context) {
	username := c.Request.FormValue("username")
	password := c.Request.FormValue("password")
	role := c.Request.FormValue("role")
	user, _ := dao.CheckUser(username)
	if user.UserID <= 0 && user.Role != role {
		zap.L().Error("用户不存在")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "用户不存在")
		return
	} else if !encryptPassword.BcryptCheck(password, user.Password) {
		zap.L().Error("密码不正确")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "密码不正确")
		return
	}
	tokenString, _ := jwt.GenerateToken(user)
	resp.ResponseSuccess(c, gin.H{
		"token": tokenString,
		"user":  user,
	})
}

func GetUsers(c *gin.Context) {
	username := c.Request.FormValue("username")
	currentPageStr := c.Request.FormValue("current_page")
	if !IsNumeric(currentPageStr) {
		zap.L().Info("页码输入不合法")
		resp.ResponseErrorWithMsg(c, resp.CodeInvalidParams, "页码输入不合法")
		return
	}
	currentPage, _ := strconv.Atoi(currentPageStr)
	users, list, err := logic.GetUsers(username, currentPage)
	if err != nil {
		zap.L().Info("查询用户列表出错了")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, err.Error())
		return
	}
	// 发送响应
	resp.ResponseSuccess(c, gin.H{
		"users": users,
		"page":  list,
	})
}

func GetUserInfo(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Request.FormValue("user_id"))
	user, err := logic.GetUserInfo(userId)
	if err != nil {
		zap.L().Info("查询用户详情出错了")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, err.Error())
		return
	}
	// 发送响应
	resp.ResponseSuccess(c, user)
}
func EditUserRole(c *gin.Context) {
	//可以修改用户名,头像,手机号,以及角色信息
	userId, _ := strconv.Atoi(c.PostForm("user_id"))
	role := c.PostForm("role")
	if err := logic.EditUserRole(userId, role); err != nil {
		zap.L().Error("修改用户权限失败", zap.Error(err))
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "修改用户权限失败")
		return
	}
	// 发送响应
	resp.ResponseSuccess(c, resp.CodeSuccess)
}

func UploadPicture(c *gin.Context) {
	// 处理上传的图片
	avatar, err := c.FormFile("file")
	if err != nil {
		zap.L().Error("获取文件失败", zap.Error(err))
		resp.ResponseError(c, resp.CodeInvalidParams)
		return
	}
	url, uploadErr := oss.UploadPic(avatar.Filename, avatar)
	if uploadErr != nil {
		zap.L().Error("上传用户头像失败", zap.Error(uploadErr))
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "上传用户头像失败")
		return
	}
	resp.ResponseSuccess(c, gin.H{
		"avatar": url,
	})
}

func EditUsers(c *gin.Context) {
	//可以修改用户名,头像,手机号,以及角色信息
	userId, err := strconv.Atoi(c.PostForm("user_id"))
	username := c.PostForm("username")
	phone := c.PostForm("phone")
	// 处理上传的图片
	url := c.PostForm("avatar")
	if err != nil {
		zap.L().Error("获取文件失败", zap.Error(err))
		resp.ResponseError(c, resp.CodeInvalidParams)
		return
	}
	//url, uploadErr := oss.UploadPic(avatar.Filename, avatar)
	//if uploadErr != nil {
	//	zap.L().Error("上传用户头像失败", zap.Error(uploadErr))
	//	resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "上传用户头像失败")
	//	return
	//}
	if err := logic.EditUser(userId, username, phone, url); err != nil {
		zap.L().Error("修改个人信息失败", zap.Error(err))
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "修改个人信息失败")
		return
	}
	// 发送响应
	resp.ResponseSuccess(c, resp.CodeSuccess)
}

func EditUserPassword(c *gin.Context) {
	userId, _ := strconv.Atoi(c.PostForm("user_id"))
	oldPassword := c.PostForm("old_password")
	newPassword := c.PostForm("new_Password")
	// 处理上传的图片
	if err := logic.EditUserPassword(userId, oldPassword, newPassword); err != nil {
		zap.L().Error("修改密码失败", zap.Error(err))
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, err.Error())
		return
	}
	// 发送响应
	resp.ResponseSuccess(c, resp.CodeSuccess)
}

func DeleteUser(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Request.FormValue("user_id"))
	user, err := logic.GetUserInfo(userId)
	if err != nil {
		zap.L().Info("查询用户详情出错了")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, err.Error())
		return
	}
	if user.Status == "删除" {
		zap.L().Info("用户已经被删除")
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, user.Username+"用户已经被删除")
		return
	}
	if err = dao.DeleteUser(userId); err != nil {
		zap.L().Error("用户删除失败", zap.Error(err))
		resp.ResponseErrorWithMsg(c, resp.CodeServerBusy, "用户删除失败")
		return
	}
	resp.ResponseSuccess(c, resp.CodeSuccess)
}
